import { Page } from "@/entity/Page";
import PmsProduct from "@/entity/PmsProduct";
import ProductQueryParam from "@/entity/ProductQueryParam";
import { ResponseResult } from "@/entity/ResponseResult";
import { AxiosResponse } from "axios";
import httpClient from "./http-client";
import { db_product } from "@/database/product";

export class ProductService {
    baseUrl = "/api/v1.0/portal/product";

    public async page(no: number = 1, size: number = 10) {
        const res = db_product.filter((it, idx) => {
            return idx >= (no - 1) * size && idx < no * size;
        });
        return Promise.resolve({
            "content": res,
            "pageable": {
                "sort": {
                    "empty": false,
                    "sorted": true,
                    "unsorted": false
                },
                "offset": 0,
                "pageNumber": no - 1,
                "pageSize": size,
                "unpaged": false,
                "paged": true
            },
            "totalElements": db_product.length,
            "last": no == db_product.length / size + 1,
            "totalPages": db_product.length / size + 1,
            "size": size,
            "number": no,
            "sort": {
                "empty": false,
                "sorted": true,
                "unsorted": false
            },
            "numberOfElements": res.length,
            "first": no - 1 == 0,
            "empty": res.length == 0
        });

        // // 前端从1开始计数，后端从0开始计数
        // no = no - 1;
        // return httpClient.get(`${this.baseUrl}?page=${no}&size=${size}`).then((res: AxiosResponse<ResponseResult<Page<PmsProduct>>>) => {
        //     if (res.data.data) {
        //         const page = res.data.data;
        //         // 前端从1开始计数，后端从0开始计数
        //         page.number = page.number + 1;
        //         return Promise.resolve(page);
        //     } else {
        //         throw "no data";
        //     }
        // });
    }

    public async query(queryParam: ProductQueryParam = {}) {
        const no = queryParam.page == null ? 1 : queryParam.page;
        const size = queryParam.size == null ? 10 : queryParam.size;

        let res = [];
        if (queryParam.categoryId != null) {
            res = db_product.filter(it => it.categoryId == queryParam.categoryId).filter((it, idx) => {
                return idx >= (no - 1) * size && idx < no * size;
            });
        } else if (queryParam.qtext != null) {
            res = db_product.filter(it => {
                return it.name && new RegExp("^.*" + queryParam.qtext + ".*$").test(it.name);
            }).filter((it, idx) => {
                return idx >= (no - 1) * size && idx < no * size;
            });
        } else {
            res = db_product.filter((it, idx) => {
                return idx >= (no - 1) * size && idx < no * size;
            });
        }
        return Promise.resolve({
            "content": res,
            "pageable": {
                "sort": {
                    "empty": false,
                    "sorted": true,
                    "unsorted": false
                },
                "offset": 0,
                "pageNumber": no - 1,
                "pageSize": size,
                "unpaged": false,
                "paged": true
            },
            "totalElements": db_product.length,
            "last": no == db_product.length / size + 1,
            "totalPages": db_product.length / size + 1,
            "size": size,
            "number": no,
            "sort": {
                "empty": false,
                "sorted": true,
                "unsorted": false
            },
            "numberOfElements": res.length,
            "first": no - 1 == 0,
            "empty": res.length == 0
        });
        // // 前端从1开始计数，后端从0开始计数
        // if (queryParam.page == null) {
        //     queryParam.page = 1;
        // }
        // queryParam.page = queryParam.page - 1;
        // if (queryParam.size == null) {
        //     queryParam.size = 10;
        // }

        // const body = {
        //     ...queryParam,
        // }

        // return httpClient.post(`${this.baseUrl}/query`, body).then((res: AxiosResponse<ResponseResult<Page<PmsProduct>>>) => {
        //     if (res.data.data) {
        //         const page = res.data.data;
        //         // 前端从1开始计数，后端从0开始计数
        //         page.number = page.number + 1;
        //         return Promise.resolve(page);
        //     } else {
        //         throw "no data";
        //     }
        // });
    }

    public async retrieve(id: number) {
        return Promise.resolve(db_product.find(it => it.id == id));
        // return httpClient.get(`${this.baseUrl}/${id}`).then((res: AxiosResponse<ResponseResult<PmsProduct>>) => {
        //     if (res.data.data) {
        //         const p = res.data.data;
        //         return Promise.resolve(p);
        //     } else {
        //         throw "no data";
        //     }
        // });
    }
}

const instance = new ProductService();
export default instance;